package com.weixin.paleituo.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.BrandSelectRecycleAdapter;
import com.weixin.paleituo.bean.Brands;
import com.weixin.paleituo.eventbus.SelectBrandEvent;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.help.RecycleViewOnItemOnclickListener;
import com.weixin.paleituo.util.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Author yichao
 * Time  2018/3/20 13:39
 * Dest  品牌选择
 */

public class BrandSelectPopuWindow extends PopupWindow {
    private List<Brands> mBrandsList=new ArrayList<>();
    private List<Integer> mIntegerList=new ArrayList<>();


    private Context mContext;
    private RecyclerView mRecyclerView;
    private BrandSelectRecycleAdapter mBrandSelectRecycleAdapter;
    private TextView mTvReset;
    private TextView mTvSure;

    public BrandSelectPopuWindow(Context context) {
        super(context);
        mContext = context;
        init();
    }
    private void init() {
        View contentView= UIUtils.inflate(mContext, R.layout.popu_brand_select);
        mRecyclerView = contentView.findViewById(R.id.recycle_popu_brand_select);
        mTvReset = contentView.findViewById(R.id.tv_reset_popu_brand_select);
        mTvSure = contentView.findViewById(R.id.tv_sure_popu_brand_select);
        //设置布局管理器
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,2));
        mBrandSelectRecycleAdapter = new BrandSelectRecycleAdapter(mContext,mBrandsList);
        //设置adapter
        mRecyclerView.setAdapter(mBrandSelectRecycleAdapter);
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        setBackgroundDrawable(new ColorDrawable(0x90000000));
        // TODO: 2016/5/17 设置可以获取焦点
        setFocusable(true);
        // TODO: 2016/5/17 设置可以触摸弹出框以外的区域
        setOutsideTouchable(true);
        // TODO：更新popupwindow的状态
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(contentView);
        mBrandSelectRecycleAdapter.setRecycleViewOnItemOnclickListener(new RecycleViewOnItemOnclickListener() {
            @Override
            public void onItemClick(View view, RecycleViewHolder viewHolder, int position) {
                int brandId = mBrandsList.get(position).getBrandId();
                if (!mIntegerList.contains(brandId)){
                    mIntegerList.add(brandId);
                }else {
                    mIntegerList.remove((Object)brandId);
                }
                mBrandSelectRecycleAdapter.setSelect(mIntegerList);
                mBrandSelectRecycleAdapter.notifyDataSetChanged();
            }
        });
        mTvReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIntegerList.clear();
                mBrandSelectRecycleAdapter.setSelect(mIntegerList);
                mBrandSelectRecycleAdapter.notifyDataSetChanged();
            }
        });

        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
               //mIntegerList
                EventBus.getDefault().post(new SelectBrandEvent(mIntegerList));




            }
        });


    }

    public BrandSelectPopuWindow(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(anchor);
    }
    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }

        super.showAsDropDown(anchor, xoff, yoff);
    }

    public  void setList(List<Brands> brandsList, List<Integer> integerList){
        mBrandsList.clear();
        mBrandsList.addAll(brandsList);
        mIntegerList.clear();
        mIntegerList.addAll(integerList);
        mBrandSelectRecycleAdapter.setSelect(integerList);
        mBrandSelectRecycleAdapter.notifyDataSetChanged();

    }


}
